package com.futures.investm.common;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.futures.investm.R;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.UpdatePasswordCallback;

import butterknife.BindView;
import butterknife.OnClick;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.RegUtils;
import tech.com.commoncore.utils.ToastUtil;
import tech.com.commoncore.widget.CountDownTextView;

public class ForgetPwActivity extends BaseTitleActivity {


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    //    @BindView(R.id.tv_get_code1)
    CountDownTextView tvGetCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_comfirm_pw)
    EditText etComfirmPw;
    @BindView(R.id.btn_find_pw)
    Button btnFindPw;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("忘记密码")
                .setOnLeftTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBackPressed();
                    }
                })
                .setStatusBarLightMode(false);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_forget_pw;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvGetCode = findViewById(R.id.tv_get_code1);
    }

    @OnClick({R.id.tv_get_code1, R.id.btn_find_pw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code1:
                //获取验证码
                getCode(etPhone.getText().toString().trim());
                break;
            case R.id.btn_find_pw:
                //确认
                resetPassword(etPhone.getText().toString().trim(), etPassword.getText().toString().trim(), etComfirmPw.getText().toString().trim(), etCode.getText().toString().trim());
                break;
        }
    }

    //获取验证码
    private void getCode(final String phone) {

        //手机号码空校验
        if (DataUtils.isEmptyString(phone)) {
            ToastUtil.show("请输入手机号");
            return;
        }
        //手机号有效性校验
        if (!RegUtils.isMobile(phone)) {
            ToastUtil.show("请输入有效手机号");
            return;
        }


        AVUser.requestPasswordResetBySmsCodeInBackground(phone, new RequestMobileCodeCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    /* 请求成功 */
                    tvGetCode.start();
                    ToastUtil.show("已发送验证码");
                } else {
                    /* 请求失败 */
                    ToastUtil.show(e.getMessage());
                }
            }
        });

    }
    //校验验证码
    private void resetPassword(final String phone, final String password, String comfirmPassword, String code) {
        //手机号码空校验
        if (DataUtils.isEmptyString(phone)) {
            ToastUtil.show("请输入手机号");
            return;
        }
        //手机号有效性校验
        if (!RegUtils.isMobile(phone)) {
            ToastUtil.show("请输入有效手机号");
            return;
        }

        //验证码空校验
        if (DataUtils.isEmptyString(code)) {
            ToastUtil.show("请输入验证码");
            return;
        }

        //密码空校验
        if (DataUtils.isEmptyString(password)) {
            ToastUtil.show("请输入新密码");
            return;
        }

        if (password.length() < 6) {
            ToastUtil.show("新密码不能少于6位");
            return;
        }

        //密码空校验
        if (DataUtils.isEmptyString(comfirmPassword)) {
            ToastUtil.show("请输入确认密码");
            return;
        }

        if (!password.equals(comfirmPassword)) {
            ToastUtil.show("两次密码不一致");
            return;
        }


        AVUser.resetPasswordBySmsCodeInBackground(code, password, new UpdatePasswordCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    //成功
                    ToastUtil.show("重置密码成功");
                    ForgetPwActivity.this.finish();
                } else {
                    //失败
                    ToastUtil.show(e.getMessage());
                }
            }
        });

    }
}
